package com.bringway.p2p.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.service.SysAttechmentService;
import com.bringway.p2p.service.ZqshcgService;



/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：债权收益界面
 */
@Controller
@RequestMapping("/zqshcg")
public class ZqshcgController extends JsonController{
	
	
	Logger logger = Logger.getLogger(PosController.class);

	
	@Autowired
	private ZqshcgService zqshcgService;
	
	/**
	 * 
	 * @param request 债权发放草稿没
	 * @param response 查询功能
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping("/queryZqshcgList")
	public ModelAndView queryZqshcgList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String xmmct = RequestUtils.getStringParameter(request, "xmmct");
		String jkrt = RequestUtils.getStringParameter(request, "jkrt");
		String state = RequestUtils.getStringParameter(request, "state");
		String shsjt = RequestUtils.getStringParameter(request, "shsjt");
		String dfridt = RequestUtils.getStringParameter(request, "dfridt");
		String xmType =  RequestUtils.getStringParameter(request, "xmType");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("shsjt", shsjt);
		params.put("state", state);
		params.put("jkrt", jkrt);
		params.put("xmmct", xmmct);
		params.put("dfridt", dfridt);
		params.put("xmType", xmType);
		List list = zqshcgService.queryZqshjgList(params);
		int count = zqshcgService.getZqshjCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * 
	 * @param request 添加功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveZqshcg")
	public ModelAndView saveZqshcg(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String xmmc = RequestUtils.getStringParameter(request, "xmmc");
		String sjsyl = RequestUtils.getStringParameter(request, "sjsyl");
		String je = RequestUtils.getStringParameter(request, "je");
		String qx = RequestUtils.getStringParameter(request, "qx");
		String qxMonth = RequestUtils.getStringParameter(request, "qxMonth");
		String fbsj = RequestUtils.getStringParameter(request, "fbsj");
		String jkr = RequestUtils.getStringParameter(request, "jkr");
//		String dfrid = RequestUtils.getStringParameter(request, "dfrid");
		String dfrid = null!=sysUserDetails?sysUserDetails.getGyh():"";//默认发标人
		String jkxmjj = RequestUtils.getStringParameter(request, "jkxmjj");
		String jkyt = RequestUtils.getStringParameter(request, "jkyt");
		String dbsm = RequestUtils.getStringParameter(request, "dbsm");
		String mjq = RequestUtils.getStringParameter(request, "mjq");
		String jxrq = RequestUtils.getStringParameter(request, "jxrq");
		String fdType = RequestUtils.getStringParameter(request, "fdType");
		String sjsylShow = RequestUtils.getStringParameter(request, "sjsylShow");
		String xmType = RequestUtils.getStringParameter(request, "xmType");//项目类型
		String hkType = RequestUtils.getStringParameter(request, "hkType");//还款类型
		String atts = RequestUtils.getStringParameter(request, "atts");//附件
		//新增借款人信息开始
		String jkrCsnf = RequestUtils.getStringParameter(request, "jkrCsnf");//出生年份
		String jkrZgxl =  RequestUtils.getStringParameter(request, "jkrZgxl");//最高学历
		String jkrHyzk =  RequestUtils.getStringParameter(request, "jkrHyzk");//婚姻状况
		String jkrZnqk =  RequestUtils.getStringParameter(request, "jkrZnqk");//子女情况
		String jkrHjdz =   RequestUtils.getStringParameter(request, "jkrHjdz");//户籍地址
		String jkrZcSrsp =  RequestUtils.getStringParameter(request, "jkrZcSrsp");//月收入水平
		String jkrZcFcqk =  RequestUtils.getStringParameter(request, "jkrZcFcqk");//房产情况
		String jkrZcXykzs =  RequestUtils.getStringParameter(request, "jkrZcXykzs");//信用卡张数
		String jkrZcXyked =  RequestUtils.getStringParameter(request, "jkrZcXyked");//信用卡额度
		String jkrDwHy =  RequestUtils.getStringParameter(request, "jkrDwHy");//行业
		String jkrDwXz =  RequestUtils.getStringParameter(request, "jkrDwXz");//性质
		String jkrDwDz =  RequestUtils.getStringParameter(request, "jkrDwDz");//地址
		String jkrDwZw =   RequestUtils.getStringParameter(request, "jkrDwZw");//职位
		String jkrDwRzsj =  RequestUtils.getStringParameter(request, "jkrDwRzsj");//入职时间
		
		//项目简介-资产类型
		String xmZcType =  RequestUtils.getStringParameter(request, "xmZcType");//资产类型
		String xmPgjz =  RequestUtils.getStringParameter(request, "xmPgjz");//评估价值(万)
		String xmSyed =  RequestUtils.getStringParameter(request, "xmSyed");//授信额度(万)
		
		params.put("jkrCsnf", jkrCsnf);
		params.put("jkrZgxl", jkrZgxl);
		params.put("jkrHyzk", jkrHyzk);
		params.put("jkrZnqk", jkrZnqk);
		params.put("jkrHjdz", jkrHjdz);
		params.put("jkrZcSrsp", jkrZcSrsp);
		params.put("jkrZcFcqk", jkrZcFcqk);
		params.put("jkrZcXykzs", jkrZcXykzs);
		params.put("jkrZcXyked", jkrZcXyked);
		params.put("jkrDwHy", jkrDwHy);
		params.put("jkrDwXz", jkrDwXz);
		params.put("jkrDwDz", jkrDwDz);
		params.put("jkrDwZw", jkrDwZw);
		params.put("jkrDwRzsj", jkrDwRzsj);
		//新增借款人信息结束
		params.put("xmmc", xmmc);
		params.put("jxrq", jxrq);
		params.put("sjsyl", sjsyl);
		params.put("je", je);
		params.put("qx", qx);
		params.put("qxMonth", qxMonth);
		params.put("fbsj", fbsj);
		params.put("jkr", jkr);
		params.put("dfrid", dfrid);
		params.put("jkxmjj", jkxmjj);
		params.put("jkyt", jkyt);
		params.put("dbsm", dbsm);
		params.put("mjq", mjq);
		params.put("jxrq", jxrq);
		params.put("fdType", fdType);
		params.put("sjsylShow", sjsylShow);
		params.put("xmType", xmType);
		params.put("hkType", hkType);
		params.put("atts", atts);
		params.put("xmZcType", xmZcType);
		params.put("xmPgjz", xmPgjz);
		params.put("xmSyed", xmSyed);
		try{
			zqshcgService.saveZqshj(params);
			result.put("success", true);
			result.put("isAddSuc", true);
		}catch(Exception e){
			e.printStackTrace();
			logger.info("saveZqshcg 发标申请保存失败! " + e.getMessage());
			result.put("success", true);
			result.put("isAddSuc", false);
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * 
	 * @param request 删除功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteZqshcg")
	public ModelAndView deleteZqshcg(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String bizId = RequestUtils.getStringParameter(request, "bizId");
		String gyh = sysUserDetails.getGyh();
		System.out.println(gyh);
		params.put("bizId", bizId);
		params.put("gyh", sysUserDetails.getGyh());
		params.put("username", sysUserDetails.getUsername());
		try{
			Map<String,Object> zqInfoMap = zqshcgService.getZqInfo(params);
			if(null !=zqInfoMap){
				if(zqInfoMap.containsKey("STATE") && "01".equals(zqInfoMap.get("STATE")) && gyh.equals(zqInfoMap.get("LDU"))){
					zqshcgService.deleteZqshcg(params);
					result.put("isDeleteSuc", true);
					result.put("msg", "删除成功!");
				}else{
					result.put("isDeleteSuc", false);
					result.put("msg", "对不起,无法删除非本人申请记录!");
				}
			}else{
				result.put("isDeleteSuc", false);
				result.put("msg", "对不起,信息不存在!");
			}
		}catch(Exception e){
			e.printStackTrace();
			result.put("isDeleteSuc", false);
			result.put("msg", "对不起,数据异常!");
			logger.info("deleteZqshcg 删除zqinfo失败！"+ params.toString());
		}
		result.put("success", true);
		serialize(request,response,result);
		return null;
	}
	
	/**
	 * 
	 * @param request  草稿审核
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveSHZqshcg")
	public ModelAndView saveSHZqshcg(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		result.put("success", true);
		JSONObject params = new JSONObject();
		String id = RequestUtils.getStringParameter(request, "zq_cg_id");
		String shr = RequestUtils.getStringParameter(request, "shr");
		String shState = RequestUtils.getStringParameter(request, "sh_state");
		String shContent = RequestUtils.getStringParameter(request, "sh_content");
		params.put("id", id);
		params.put("shr", shr);
		params.put("shState", shState);
		params.put("shContent", shContent);
			Map<String, Object> zqshcgMap = zqshcgService.getZqshcg(params);
			if(null == zqshcgMap){
				result.put("isSHSuc", false);
				result.put("msg", "信息有误！");
			}else if(zqshcgMap.containsKey("STATE")){
				Object stateObj = zqshcgMap.get("STATE");
				if(null != stateObj && stateObj.toString().equals("01")){//待审核状态才可以审核
					try{
						boolean isSHSuc = zqshcgService.saveSHZqshcg(params);
						result.put("isSHSuc", isSHSuc);
						if (!isSHSuc) {
							result.put("msg", "审核失败！");
						}
					}catch (Exception e) {
						e.printStackTrace();
						result.put("isSHSuc", false);
						result.put("msg", "审核失败！");
					}
				}else{//无审核状态信息,无法审核
					result.put("isSHSuc", false);
					result.put("msg", "该申请已审核过，请勿重复操作！");
				}
			}
		serialize(request,response,result);
		return null;
	}
	
	/**
	 * 获得分档类别数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/loadFdType")
	public ModelAndView loadFdType(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		List<Map<String,String>> list = this.zqshcgService.loadFdType();
		serialize(request,response,list);
		return null;
	}
	
	
	/**
	 * 申请发放债权，根据期限(月)，获得实际天数
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getQxDayByQxMonth")
	public ModelAndView getQxDayByQxMonth(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		String qxMonth = RequestUtils.getStringParameter(request, "qxMonth");
		int qxDay = this.zqshcgService.getQxDayByQxMonth(qxMonth);
		if(qxDay > 0){
			result.put("success", true);
			result.put("msg", "获取实际期限(天)成功！");
			result.put("qxDay", qxDay);
		}else{
			result.put("success", false);
			result.put("msg", "获取实际期限(天)失败！");
			result.put("qxDay", qxDay);
		}
		serialize(request,response,result);
		return null;
	}
	
}
